package com.cloudm.technician.ui.modules.common.login

import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.extensions.string
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BaseFragment
import com.cloudm.technician.ui.modules.common.home.HomeActivity
import com.cloudm.technician.ui.modules.common.register.RegisterActivity
import kotlinx.android.synthetic.main.fragment_login.*

/**
 * 作者: wesley
 * 时间: 2017/12/14
 * 描述: ui
 */
class LoginFragment : BaseFragment<LoginPresenter>(), LoginContract.View {

    companion object {
        fun newInstance() = LoginFragment()
    }

    override val resource: Int
        get() = R.layout.fragment_login

    override fun initInject() {
        DaggerLoginComponent.builder().appComponent(appComponent).loginPresenterModule(
                LoginPresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {

    }

    override fun initVariables() {

    }

    override fun initViews(view: View) {

    }

    override fun getPhoneNumber(): String? = etPhoneNumber.string()

    override fun getPassword(): String? = etPassword.string()

    override fun btnLoginEnabled() {
        btnLogin.isEnabled = true
        btnLogin.setTextColor(ContextCompat.getColor(mContext, R.color.common_color_ffffff))
        btnLogin.setBackgroundResource(R.drawable.drawable_button)
    }

    override fun btnLoginNotEnabled() {
        btnLogin.isEnabled = false
        btnLogin.setTextColor(ContextCompat.getColor(mContext, R.color.color_white_alpha_80))
        btnLogin.setBackgroundResource(R.drawable.drawable_button_alpha)
    }

    override fun setIvClearPhoneVisible() {
        llClearPhoneNumber.visibility = View.VISIBLE
    }

    override fun setIvClearPhoneNotVisible() {
        llClearPhoneNumber.visibility = View.INVISIBLE
    }

    override fun showInputPhoneNumberMsg() {
        mContext.toast(mContext.resources.getString(R.string.please_input_phone_number))
    }

    override fun showPhoneNumberFormatMsg() {
        mContext.toast(mContext.resources.getString(R.string.phone_number_format_is_error))
    }

    override fun showPasswordEmptyMsg() {
        mContext.toast(mContext.resources.getString(R.string.please_input_password))
    }

    override fun loginFailure(errorMsg: String) {
        mContext.toast(errorMsg)
    }

    override fun loginSuccess() {
        mContext.openActivity(HomeActivity::class.java, false)
        val activity = mContext as? LoginActivity ?: return
        activity.finish()
    }

    override fun showUserEmptyMsg() {
        mContext.toast(mContext.resources.getString(R.string.no_user_msg))
    }

    override fun showLoginDialog() {
        showLoadingDialog(mContext.resources.getString(R.string.loging))
    }

    override fun hideLoginDialog() {
        hideLoadingDialog()
    }

    override fun initListener() {
        tvForgePassword.setOnClickListener(OnceClickListener())
        llClearPhoneNumber.setOnClickListener(OnceClickListener())
        btnLogin.setOnClickListener(OnceClickListener())
        etPhoneNumber.addTextChangedListener(TextChangeListener(0))
        etPassword.addTextChangedListener(TextChangeListener(1))
        etPhoneNumber.onFocusChangeListener = LoginFocusChangeListener()
    }


    private inner class OnceClickListener : com.cloudm.technician.ui.widget.OnceClickListener() {

        override fun onOnceClick(view: View) {
            when (view.id) {
                R.id.btnLogin -> {
                    presenter?.login(getPhoneNumber(), getPassword())
                }

                R.id.tvForgePassword -> {
                    val intent = Intent(mContext, RegisterActivity::class.java).apply {
                        putExtras(Bundle().apply {
                            putInt("flag", 1)
                        })
                    }
                    mContext.openActivity(intent, false)
                }

                R.id.llClearPhoneNumber -> etPhoneNumber.setText("")
            }
        }
    }


    //监听文本改变
    private inner class TextChangeListener(private val flag: Int) : TextWatcher {

        override fun afterTextChanged(s: Editable?) {

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            presenter?.updateBtnStatus()
            presenter?.updateIvClearPhoneStatus(flag, s)
        }
    }

    //焦点改变
    private inner class LoginFocusChangeListener : View.OnFocusChangeListener {
        override fun onFocusChange(v: View?, hasFocus: Boolean) {
            presenter?.updateIvClearPhoneStatus(hasFocus)
        }
    }
}